<!--
 * @Author: ZhaoZhiqi
 * @Date: 2022-09-20 09:54:19
 * @LastEditors: ZhaoZhiqi
 * @LastEditTime: 2022-12-13 15:29:53
 * @Description: 虚拟大厅
 * @FilePath: \tuanfeng-mini\src\pages\vr.vue
-->
<template>
    <div class="container">
        <web-view class="webview" :src="url"></web-view>
        <!-- <cover-view class="btn">
            <cover-image class="btn-img" src="http://47.108.13.46:8083/profile/static/20221009153842-service.png"></cover-image>
            <button class="btn-text" open-type="contact" bindcontact="handleContact" session-from="sessionFrom">人工客服</button>
        </cover-view> -->
        <cover-view class="btn btn-robot" @click="change">
            <cover-image class="btn-img" src="https://tuanfeng.whkxzj.com/profile/static/20231127105532-robot.png"></cover-image>
            <cover-view class="btn-text">智能导服</cover-view>
        </cover-view>
        <cover-view v-if="visible" class="cover-view">
            <cover-view class="list">
                <cover-view class="group" v-for="group in sortList">
                    <cover-view class="group-title">{{group.k}}</cover-view>
                    <cover-view class="group-list">
                        <template v-for="item in group.v">
                            <cover-view class="item" @click="toPage(item)">{{item.name}}</cover-view>
                            <cover-view class="border"></cover-view>
                        </template>
                    </cover-view>
                </cover-view>
            </cover-view>
        </cover-view>
    </div>
</template>
<script>
import { hallPVAPI } from "../api/stat";
import { getDeptAPI } from "../api/department";

export default {
    data() {
        return {
            baseUrl: "https://22fl0yqf1.720think.com/t/22fl0yqf1",
            url: "",
            value: "",
            visible: false,
            list: [], // 政务部门
            sortList: [],
        };
    },
    onShareAppMessage() {},
    onLoad(options) {
        let { def_sid } = options;
        this.url = def_sid
            ? this.baseUrl + "?def_sid=" + def_sid
            : this.baseUrl;

        getDeptAPI({}).then((res) => {
            this.list = res.data;
            this.sortList = this.sort(this.list);
        });
    },
    onShow() {
        // 虚拟大厅pv
        hallPVAPI();
    },
    methods: {
        change() {
            this.visible = !this.visible;
        },
        toPage(item) {
            this.$u.route({
                url: "pages/chat",
                params: {
                    id: item.id,
                    title: item.name,
                },
            });
        },
        onChange(value) {
            if (value) {
                this.curList = this.list.filter((item) => {
                    return item.name.indexOf(value) !== -1;
                });
            } else {
                this.curList = [];
            }
        },

        _chineseToEnglish(c) {
            var idx = -1;
            var MAP = "ABCDEFGHJKLMNOPQRSTWXYZ";
            var boundaryChar = "驁簿錯鵽樲鰒餜靃攟鬠纙鞪黁漚曝裠鶸蜶籜鶩鑂韻糳";
            if (!String.prototype.localeCompare) {
                throw Error("String.prototype.localeCompare not supported.");
            }
            if (/[^\u4e00-\u9fa5]/.test(c)) {
                return c;
            }
            for (var i = 0; i < boundaryChar.length; i++) {
                if (
                    boundaryChar[i].localeCompare(c, "zh-CN-u-co-pinyin") >= 0
                ) {
                    idx = i;
                    break;
                }
            }
            return MAP[idx];
        },
        //获取第一个字母
        _getFirstUpperChar(str) {
            var string = String(str);
            var c = string[0];
            if (/[^\u4e00-\u9fa5]/.test(c)) {
                return c.toUpperCase();
            } else {
                return this._chineseToEnglish(c);
            }
        },
        //排序
        sort(list) {
            let that = this;
            let map = {};
            map["#"] = [];
            var c = "A".charCodeAt();
            for (; c <= "Z".charCodeAt(); c++) {
                map[String.fromCharCode(c)] = [];
            }
            var firstCharUpper;
            list.forEach(function (item) {
                firstCharUpper = that._getFirstUpperChar(item.name);
                if (map.hasOwnProperty(firstCharUpper)) {
                    map[firstCharUpper].push(item);
                } else {
                    map["#"].push(item);
                }
            });
            for (let a in map) {
                if (!map[a].length) {
                    delete map[a];
                }
            }
            let sortlist = [];
            for (let key in map) {
                sortlist.push({
                    k: key,
                    v: map[key],
                });
            }
            return sortlist;
        },
    },
};
</script>
<style scoped lang="scss">
@import "../assets/scss/common.scss";

.btn {
    position: fixed;
    right: 15px;
    top: 110px;
    width: 100px;
    height: 30px;
    border-radius: 6px;
    background-color: rgba(255, 255, 255, 0.8);
    text-align: center;
    z-index: 100;
    display: flex;
    justify-content: center;
    align-items: center;
    &.btn-robot {
        // top: 95px;
    }
    .btn-img {
        width: 20px;
        height: 20px;
        margin-right: 5px;
        flex: none;
    }
    .btn-text{
        font-size: 14px;
        color: $color_primary-text;
        background-color: transparent;
        margin: 0;
        &::after{
            border: none;
        }
    }
}
.cover-view {
    position: relative;
    width: 50vw;
    height: 30vh;
    background-color: #fff;
    position: fixed;
    // top: 130px;
    top: 145px;
    right: 15px;
    z-index: 10000;
    overflow-y: scroll;
    border-radius: 5px;
    .list {
        margin: 0 10px;
        .group {
            margin-bottom: 5px;
            .group-title {
                font-size: 13px;
                margin: 5px 0;
                font-weight: bold;
            }
            .group-list {
                .item {
                    padding: 10px 0;
                    font-size: 12px;
                }
                .border {
                    width: 100%;
                    height: 1px;
                    background-color: #efefef;
                }
            }
        }
    }
}
</style>
